// distFromCoast.do
// Inputs: ElevDataIn`state'.xlsx, extra_coastal.txt, coords.xlsx
// Outputs: distancefull.dta, 1km_us_buffer_coor.dta
// Date last updated: 1/27/2025

//  Each property's distance from the nearest coastline is calculated using its precise coordinates (given in the CoreLogic data) and NOAA's Continuously Updated Shoreline Product (https://coast.noaa.gov/digitalcoast/data/cusp.html)
// Additionally, we downloaded the shape files of US medium shoreline and make a list of coordinates within 1km of the shoreline

********************************************************************************	
* Get distance from coast
********************************************************************************
// Putting together distance from coast for each lat/long pair in each state 
// Distances calculated in ArcGIS

// Loop through each state and save property lat/long pairs as temporary files
	foreach state in CT DE FL GA MA MD ME NC NH NJ NY PA RI SC VA{
		import excel "$pathr\ElevDataIn`state'.xlsx", sheet("Sheet1") firstrow clear
		tempfile elevData`state'
		save `elevData`state''
	}

// Import distances generated in ArcGIS, save temp file
	import delimited "$pathr\extra_coastal.txt", clear
	drop x y
	tempfile extraCoastal
	save `extraCoastal'
	
// Import coordinates of distances and merge to extra coastal, save
	import excel "$pathr\coords.xlsx", sheet("Sheet1") firstrow clear
	merge 1:1 num using `extraCoastal'
	tempfile distanceEXT
	save `distanceEXT'

// Append all states' coordinate data to each other
	use `elevDataCT', clear
	foreach state in DE FL GA MA MD ME NC NH NJ NY PA RI SC VA{
		append using `elevData`state''
	}

// Add in distance from coast, rounding to make sure lat/long match properly
	append using `distanceEXT'
	replace x = round(x, .00001)
	replace y = round(y, .00001)

// Keep only one pair of lat/long coordinates, save
	duplicates drop x y, force
	drop _merge
	save "$pathi\distancefull", replace 

********************************************************************************	
* Make list of coordinates within 1km buffer for whole US
********************************************************************************
// Get coordinates of shape file from NOAA US medium shoreline
	shp2dta using "$pathr\us_medium_shoreline_1km_buffer.shp", coor("$pathi\1km_us_buffer_coor") replace